package itheima_edu_info_manager.com.itheima.edu.info.manager.service;

import itheima_edu_info_manager.com.itheima.edu.info.manager.controller.OtherStudentController;
import itheima_edu_info_manager.com.itheima.edu.info.manager.dao.OtherStudentDao;
import itheima_edu_info_manager.com.itheima.edu.info.manager.dao.StudentDao;
import itheima_edu_info_manager.com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    //将studentDao变为成员变量
    private OtherStudentDao studentDao = new OtherStudentDao();

    //添加学生
    public boolean addStudent(Student stu) {
        return studentDao.addStudent(stu);
    }

    //判断输入的id是否存在
    public boolean isExists(String id) {
        //假设输入的id不存在
        boolean exists = false;
        Student[] stu = studentDao.findAllStudent();
        for (int i = 0; i < stu.length; i++) {
            Student student = stu[i];
            //判断数组是否为null，数组是否存在输入的id
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    //查询学生
    public Student[] findAllStudent() {
        Student[] student = studentDao.findAllStudent();
        boolean flag = false;
        for (int i = 0; i < student.length; i++) {
           Student st = student[i];
           if (st != null){
               flag = true;
               break;
           }
        }
        if (flag){
            return student;
        }else {
            return null;
        }
    }

    //删除学生
    public void deleteStudentById(String delid) {
        studentDao.deleteStudentById(delid);
    }

    //修改学生
    public void upDateStudentById(String id, Student stu) {
        studentDao.upDateStudentById(id,stu);
    }
}
